package pers.zdb.ag.servlet;

import com.alibaba.fastjson.JSON;
import pers.zdb.ag.AddressUtils;
import pers.zdb.ag.pojo.Address;
import pers.zdb.ag.util.IpUtils;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description:
 * @author: Adobe Chow
 * @date: 2018/8/9 10:10
 * @Copyright: www.winshang.com Inc. All rights reserved.
 */
public class AddressServlet extends javax.servlet.http.HttpServlet {
    protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
            doGet(request,response);
    }

    protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        long ttt = System.currentTimeMillis();
        Map map = new HashMap();
        try{
            //记录用户访问

        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");
        request.setCharacterEncoding("UTF-8");

        String result = "";
        String location = request.getParameter("location");

        if (location==null) location="";

        if (location!=null&&location.trim().length()>0){
            if(location.indexOf(",")==-1){
                result = "经纬度请用英文逗号隔开(如 113.575895,23.116969 )";

                map.put("status",-1);
                map.put("success",false);
                map.put("result",result);

            }else{
                double longitude = Double.parseDouble(location.split(",")[0]);
                double latitude = Double.parseDouble(location.split(",")[1]);
                Address address = AddressUtils.getAddress(longitude,latitude);
                result = address.getProvince()+"-"+address.getCity()+"-"+address.getArea();

                map.put("status",1);
                map.put("success",true);
                map.put("result",result);

            }
        }else {
            result = "请输入经纬度";
            map.put("status",-2);
            map.put("success",false);
            map.put("result",result);
        }

        }catch (Exception e){
            map.put("status",-100);
            map.put("success",false);
            map.put("result","系统错误");
        }

        /**
         * 返回json
         */
        PrintWriter out = response.getWriter();
        out.write(JSON.toJSONString(map));
        out.close();


        //记录用户访问
        String ip = IpUtils.getIpAddr(request);
        if(!new File("/user-log").exists()){
            new File("/user-log").mkdir();
        }

        PrintWriter printWriter = new PrintWriter(new FileWriter("/user-log/address.log",true));
        printWriter.println("DATE:"+new Date());
        printWriter.println("IP:"+ip);
        printWriter.println("useTime:"+(System.currentTimeMillis()-ttt));
        printWriter.println();
        printWriter.close();

    }
}
